<template>
  <div class="goods-hot">
    <h3>{{ titleObj[type] }}</h3>
    <!-- 商品区块 -->
    <goods-item
      v-for="good in goodsList"
      style="width: 100%"
      :goods="good"
      :key="good.id"
    />
  </div>
</template>
<script>
import { ref, onMounted } from 'vue'
import { findHotGoods } from '@/api/goods'
import { useRoute } from 'vue-router'
import GoodsItem from '@/views/Category/components/goods-item'

// 获取榜单数据
const useHot = (props) => {
  // 商品列表
  const route = useRoute()
  const goodsList = ref([])
  onMounted(() => {
    findHotGoods({ id: route.params.id, type: props.type }).then(data => {
      goodsList.value = data.result.map(item => {
        // 字段一致性处理
        item.tag = item.desc
        return item
      })
    })
  })
  return { goodsList }
}
export default {
  name: 'GoodsHot',
  components: {
    GoodsItem
  },
  props: {
    type: {
      type: Number,
      default: 1
    }
  },
  setup (props) {
    // 不是响应数据
    const titleObj = { 1: '24小时热销榜', 2: '周热销榜', 3: '总热销榜' }

    const { goodsList } = useHot(props)

    return { titleObj, goodsList }
  }
}
</script>
<style scoped lang="less">
.goods-hot {
  h3 {
    height: 70px;
    background: @helpColor;
    color: #fff;
    font-size: 18px;
    line-height: 70px;
    padding-left: 25px;
    margin-bottom: 10px;
    font-weight: normal;
  }
}
</style>
